/**
 * Used to instantiate rook pieces that are used in the chess game. Rook extends Piece and inherits the instance
 * variables that are common to all the pieces in the chess game
 * 
 * @author jmikhail
 * @version 1.12
 */
package pieces;
import mainClasses.*;
import java.lang.Math;
public class Rook extends Piece{

	/**
	 * Set to true if a piece is blocking the movement of the queen on the board
	 */
	public boolean blockage = false;
	public boolean firstMove = true;
	
	
	/**
	 * Constructor for the class Rook. It creates a new Rook piece with the specified color at the 
	 * coordinates given and places it on board b.
	 * 
	 * @param xaxis The position of the piece on the board in the direction of the X axis
	 * @param yaxis The position of the piece on the board in the direction of the Y axis
	 * @param b Board on which the piece is used
	 * @param color Color or side of the piece
	 */
	public Rook(int xaxis, int yaxis, Board board, String color)
	{
		super(xaxis, yaxis, board,  "  Rook   ", color);
		if(color!="White")
		{
			super.type = "BK-->Rook";
			
		}
	}
	
	
	
	
	
	/**
	 * Checks if the movement to the given point is in the allowed directions of the queen. i.e., vertical or horizontal
	 * If possible, it checks if there is another piece in the path blocking the movement and if there is a piece of 
	 * the same color at the destination. If the movement is allowed, it returns true.
	 * 
	 * @param p2 Destination of the movement
	 * @return A boolean that is set to true if the move to the point given is allowed
	 */
	public boolean movePossible(int desX, int desY)
	{
		int inx = this.x;
		int iny = this.y;
		if(desX>7||desX<0||desY>7||desY<0)
        {
        	return false; 	//Returns false if the input is invalid (out of bounds)
        }else if (Math.abs(inx - desX) > 0 && Math.abs(iny - desY) == 0) 	//Horizontal Movement
		{
			if (inx > desX) {										//Moving to the left
				for (int i = desX; i <= inx; i++) {
					if (i != desX && i != inx && !this.board.getPiece(i, this.y).getType().equals("         "))
					{
						blockage = true;
					}
				}
			} else if (desX > inx) {								//Moving to the right
				for (int i = inx; i <= desX; i++) {
					if (i != desX && i != inx && !this.board.getPiece(i, this.y).getType().equals("         ")) {
						blockage = true;
					}
				}
			}
			if (blockage == false) {								//If there is no blockage, return true
				if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) {
					return true;
				}
			}
			blockage = false;
			return false;											//If there is a blockage, return false
		} 
		else if (Math.abs(inx - desX) == 0 && Math.abs(iny - desY) > 0) {	//Vertical Movement
			if (iny > desY) {												//Moving downwards
				for (int i = desY; i <= iny; i++) {
					if (i != desY && i != iny && !(this.board.getPiece(this.x, i) instanceof NullPiece)) {
						blockage = true;
					}
				}
			} else if (desY > iny) {									//Moving upwards
				for (int i = iny; i <= desY; i++) {
					if (i != desY && i != iny && !(this.board.getPiece(this.x, i) instanceof NullPiece)) {
						blockage = true;
					}
				}
			}
			if (blockage == false) 									//If there is no blockage, return true
			{
				if (this.board.getPiece(desX, desY) instanceof NullPiece || this.isEnemy(desX, desY)) 
				{
					return true;
				}
			}
			blockage = false;
			return false;
		} 
		return false;
	}





	public void setFirstMove(boolean firstMove) {
		this.firstMove = firstMove;
	}





	public boolean isFirstMove() {
		return firstMove;
	}
}